var app = getApp();
var times = require('../../../utils/util.js');
Page({
  data: {
    starCount: 0,
    cardCur: 0,
    swiperList: [],
    goods:{},
    dealer:wx.getStorageSync('dealer'),
    opens:0,
    specsIndex:0,//当前选择的规格索引
    num:1,//选择的数量
    timea:'',
    cartnum:'',//购物车数量
    colls:0//是否收藏
  },
  onLoad(options) {
  	var $th=this;
		//返回减天数 时间范围
		var time=new Date()
		var getdataw = function (tt, num) {
		    var iToDay = tt.getDate();
		    var iToMon = tt.getMonth();
		    var iToYear = tt.getFullYear();
		    var newDay = new Date(iToYear, iToMon, (iToDay + num));
		    return newDay
		}
		
    this.setData({
    	dealer : wx.getStorageSync('dealer'),
      timea: times.formatTime(getdataw(time,1),'M月D日')
    })
    //end
    //购物长度
		var cartnum=0;
		var cart=wx.getStorageSync('cart');
		for(var i in cart){
			cartnum++;
		}
		if(cartnum>0){
			this.setData({
	    	cartnum:cartnum
	    });
		}
		//end
    var post={
    		id:options.id
    	}
    	app.globalAjax(
	  		'/api/goods/detail',
	  		"get",
	  		post,
	  		function(e){
	  			if(e.code==0)
	      	{
	      		wx.setNavigationBarTitle({
					 	 title: e.goods.goodsName 
						});
						
						if(1){//无规格商品
							e.goods.specsList=[{
								id: e.goods.specsId,
								parentId: '',
								price: e.goods.promotionPrice,
								specsName: e.goods.specsName
							}]
						}
						
	      		$th.setData({
	      				goods:e.goods,
					      swiperList:e.goods.picUrls
					  })
	      		$th.towerSwiper('swiperList');
	      		var that=$th.data
	      		var coll=wx.getStorageSync('coll')
	      		var sets=coll[that.goods.categoryId+"_"+that.goods.goodsId+"_"+that.goods.specsList[that.specsIndex].id]
	      		if(sets){
	      			$th.setData({
	      				colls:1
					  	})
	      		}
	      		  // 初始化towerSwiper 传已有的数组名即可
	      	}else{
	      		wx.showToast({
				        title: e.msg,
				        icon: "none"
				      });
	      	}
	  	})
    
    
  },
  setopens(){//展开规格选择
  	console.log(this.data.opens);
  	var s=this.data.opens?0:1;
  	console.log(s);
  	this.setData({
      opens: s
    })
  },specSelect(e){//选择规格
  	console.log(e)
  	this.setData({
      specsIndex: e.target.dataset.index
    })
  },
  numadd(){//增加数量
  	var num=this.data.num;
  	var goods=this.data.goods
  	num++;
  	console.log(goods.stock)
  	if(num>goods.stock) {
  		wx.showToast({
        title: '库存不足',
        icon: "none"
      });
  		num=goods.stock;
  	}
  	console.log(num)
  	this.setData({
      num: num
    })
  },
  numlower(){//减少数量
  	var num=this.data.num;
  	num--;
  	if(num<1) num=1;
  	console.log(num)
  	this.setData({
      num: num
    })
  },
  suborder(){//确认下单
  	var that=this.data;
  	console.log(that.goods)
  	console.log(that.specsIndex)
  	console.log(that.num)
  	
 
  	if(that.goods.stock<=0){
  		wx.showToast({
        title: "库存不足",
        icon: "none"});
  		return false;
  	}
  	
  	var total=(that.goods.specsList[that.specsIndex].price * that.num);
  	total=parseFloat(total).toFixed(2)
  	var orderdata={
			  orderGoodsList: [
			    {
			      "goodsId":that.goods.id,//商品ID
			      "goodsName": that.goods.goodsName,//商品名称
			      "num": that.num,//数量
			      "picUrl": that.goods.picUrl,//图片
			      "price": that.goods.specsList[that.specsIndex].price,//价格
			      "specsId": that.goods.specsList[that.specsIndex].id,//规格id
			      "specsName": that.goods.specsList[that.specsIndex].specsName//规格名称
			    }
			  ],
			  total:total,//合计
			  remark: "",//备注
			  storeId: 0,//供应商ID
			  totalAmount: 0,//总金额
			}
  		wx.setStorage({
			   key: "orderlist",
			   data: orderdata //需要存储的数据
			})
	  	wx.redirectTo({
			  url: '/pages/cart/pay/pay'
			})
  	// 
  },
  addcart(){//添加购物车
  	var cart=wx.getStorageSync('cart')
  	var that=this.data;
  	
  	
  	if(that.goods.stock<=0){
  		wx.showToast({
        title: "库存不足",
        icon: "none"});
  		return false;
  	}
  	
  	
  	console.log(that)
  	var orderGoodsList={
  					"categoryId":that.goods.categoryId,//分类ID
			      "goodsId":that.goods.id,//商品ID
			      "goodsName": that.goods.goodsName,//商品名称
			      "num": that.num,//数量
			      "pic": that.goods.picUrl,//图片
			      "price": that.goods.specsList[that.specsIndex].price,//价格
			      "specsId": that.goods.specsList[that.specsIndex].id,//规格id
			      "specsName": that.goods.specsList[that.specsIndex].specsName//规格名称
			  }
  	if(!cart){
  		cart={};
  	}
  	var sets=cart[that.goods.categoryId+"_"+that.goods.goodsId+"_"+that.goods.specsList[that.specsIndex].id]
  	if(sets){
  		sets.num=(sets.num + orderGoodsList.num)
  	}else{
  		cart[that.goods.categoryId+"_"+that.goods.goodsId+"_"+that.goods.specsList[that.specsIndex].id]=orderGoodsList
  	}
  		
  		wx.setStorage({
			   key: "cart",
			   data: cart //需要存储的数据
			})
  		wx.showToast({
        title: '添加成功',
        icon: "none"
      });
  	this.setData({opens: 0 })
  	
  	//购物长度
		var cartnum=0;
		var cart=wx.getStorageSync('cart');
		for(var i in cart){
			cartnum++;
		}
		if(cartnum>0){
			this.setData({
	    	cartnum:cartnum
	    });
		}
  	
  	// 
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data[name];
    var s = []
    for (let i = 0; i < list.length; i++) {
      var swiper = {}
      swiper.picUrl = list[i];
      swiper.zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      swiper.mLeft = i - parseInt(list.length / 2)
      s.push(swiper)
    }
    this.setData({
      swiperList: s
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },
  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },
    onShareAppMessage: function(ops) {
    	var $th=this;
        if (ops.from === 'button') {
                console.log('button');
                    
        }
        return {
            title: "[乡村团购]" + $th.data.goods.goodsName,
            path: "/pages/classify/productart/productart?id="+ $th.data.goods.id,
            success: function (res) {
                // 转发成功
                console.log("转发成功:" + JSON.stringify(res));

            },
            fail: function (res) {
                // 转发失败
                console.log("转发失败:" + JSON.stringify(res));
            }
        }
    },
    coll:function(){//收藏
    	console.log(this.data.goods)
    	
    	var coll=wx.getStorageSync('coll')
	  	var that=this.data;
	  	var orderGoodsList={
	  					"categoryId":that.goods.categoryId,//分类ID
				      "goodsId":that.goods.id,//商品ID
				      "goodsName": that.goods.goodsName,//商品名称
				      "num": that.num,//数量
				      "pic": that.goods.picUrl,//图片
				      "price": that.goods.specsList[that.specsIndex].price,//价格
				      "specsId": that.goods.specsList[that.specsIndex].id,//规格id
				      "specsName": that.goods.specsList[that.specsIndex].specsName//规格名称
				  }
	  	if(!coll){
	  		coll={};
	  	}
	  	var sets=coll[that.goods.categoryId+"_"+that.goods.goodsId+"_"+that.goods.specsList[that.specsIndex].id]
	  	if(sets){
	  		
	  	}else{
	  		coll[that.goods.categoryId+"_"+that.goods.goodsId+"_"+that.goods.specsList[that.specsIndex].id]=orderGoodsList
	  	}
  		
  		wx.setStorage({
			   key: "coll",
			   data: coll //需要存储的数据
			})
  		wx.showToast({
        title: '收藏成功',
        icon: "none"
      });
    	this.setData({
    		colls:1
    	})
    	
    }
}
)